package com.ycy;


import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.*;

public class ExcelDemo5 {


    public static List<List<String>> printExcelPropertyInfo(Class<?> clazz) {
        List<List<String>> heads = new ArrayList<>();
        List<AbstractMap.SimpleEntry<String, Integer>> headIndexs = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ExcelProperty.class)) {
                ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
                System.out.println("Field: " + field.getName());
                System.out.println("Value: " + excelProperty.value());
                System.out.println("Index: " + excelProperty.index());
                AbstractMap.SimpleEntry entry = new AbstractMap.SimpleEntry(excelProperty.value()[0], excelProperty.index());
                headIndexs.add(entry);
            }
        }
        headIndexs.sort(new Comparator<AbstractMap.SimpleEntry<String, Integer>>() {
            @Override
            public int compare(AbstractMap.SimpleEntry<String, Integer> o1, AbstractMap.SimpleEntry<String, Integer> o2) {
                return Integer.compare(o1.getValue(), o2.getValue());
            }
        });
        for (AbstractMap.SimpleEntry<String, Integer> headIndex : headIndexs) {
            ArrayList<String> e = new ArrayList<>();
            e.add("注意：模版使用说明\n" +
                    "1.标题、表头请勿改动；从第5行开始填写信息；\n" +
                    "2.选项类【填写内容】不支持超出预设范围外的内容；\n" +
                    "3.不支持公式计算、合并单元格等特殊内容；\n" +
                    "4.带*为必填项。");
            e.add(headIndex.getKey());
            heads.add(e);
        }
        return heads;
    }

    public static void main(String[] args) {
        // 准备数据
        List<RiskPointerComExcel> data = new ArrayList<>();
        data.add(new RiskPointerComExcel(1, "Risk 1", "Description 1"));
        data.add(new RiskPointerComExcel(2, "Risk 2", "Description 2"));
        data.add(new RiskPointerComExcel(3, "Risk 3", "Description 3"));

        // 定义文件路径
        String fileName = "C:\\Users\\yincaiyong\\Desktop\\linshi\\r_" + System.currentTimeMillis() + ".xlsx";

        // 写入数据到Excel文件
        EasyExcel.write(fileName)
//                .registerWriteHandler(new CustomSheetWriteHandler()) // 注册自定义的SheetWriteHandler
//                .registerWriteHandler(new CustomRowWriteHandler()) //
                .head(printExcelPropertyInfo(RiskPointerComExcel.class))
                .sheet("风险点清单")
                .doWrite(data);

        String filePath = "C:\\Users\\yincaiyong\\Desktop\\linshi\\r_1727317419645.xlsx";
        filePath = fileName;
        List<RiskPointerComExcel> list = null;
        try {
            list = EasyExcel.read(new FileInputStream(filePath),

                            new AnalysisEventListener<RiskPointerComExcel>() {
                                @Override
                                public void invoke(RiskPointerComExcel data, AnalysisContext context) {
                                    System.out.println(data);
                                }
                                @Override
                                public void doAfterAllAnalysed(AnalysisContext context) {

                                }
                            }

                    )
                    .head(RiskPointerComExcel.class)
                    .sheet(0).headRowNumber(2)
                    .doReadSync();
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        }
        System.out.println(JSONUtil.toJsonStr(list));
    }


    @Data
    public static class RiskPointerComExcel {

        @ExcelProperty(value = "Name", index = 1)
        private String name;

        @ExcelProperty(value = "ID", index = 0)
        private Integer id;

        @ExcelProperty(value = "Description1", index = 2)
        private String description;

        public RiskPointerComExcel() {

        }

        // 构造函数
        public RiskPointerComExcel(Integer id, String name, String description) {
            this.id = id;
            this.name = name;
            this.description = description;
        }

        // Getter 和 Setter 方法
        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }


}
